# From assignments of UW Astro Dept. AS480.
#http://www.astro.washington.edu/courses/astro480/IRAF/iraf6_gj1111_exercise.pdf
#http://www.astro.washington.edu/courses/astro480/Exercises/exercise6program.tar.gz
#Modified by douglase@bu.edu at Advanced Teen Astronomy Camp 2013 on Kitt Peak.
#www.astronomycamp.org

#just like in IRAF, packages must be loaded
import sys, os, shutil, signal, math, types
#from photometry import *
from numpy import *
#numpy is the NUMber PYthon package that can do array math

#from pyraf import pyraf.iraf,pyraf.irafglobals
import pyraf
pyraf.iraf.images()
pyraf.iraf.imutil()
pyraf.iraf.noao()
pyraf.iraf.digiphot()
pyraf.iraf.ptools()
pyraf.iraf.daophot()

from easy_phot_params import *
#this loads all of the variables defined in easy_phot_params

def stringtohours(str):
    print(type(str))
    if type(str)==type('str'):
        hrs,mins,secs = map(float,str.split(":"))
        return hrs+mins/60. + secs/3600.
    else:
        print(str)
        return  float(str)


def execute():

    print('running')
    #here we define the coords variable as a list
    coords=[]
    
    #indenting matters in python. Everything
    #indented under the for loop is inside the loop
    f=open(coofnamein)
    for line in f.readlines():
        print(line)

        if line.find("#") !=-1:
            print('skipping comment line')
            continue
        if line.find("z1") !=-1:
            print('skipping extra leading line')
            continue
        #line.find("#")
        
        line=map(float,line.split())
        #this takes the line and splits it into a list of floats
        if len(line) != 4:
            continue
        #skips the lines that don't have 4 floats
        coords.append((line[0],line[1]))
        #adds the most recent coords (the 0th and 1st elements
        # to the list of coordinates

        #print coords
 
    coofnameout1=open(coofnameout, "w")
    print(coofnameout)
    for ci in coords:
        coofnameout1.write("%.4g\t%.4g\n" % ci)
        #the strange syntax is the formatting for the 
        #output. It prints each coordinate with 4 decimal places
        # a tab in between, and then a new line at the end
    coofnameout1.close()

    l=os.listdir(image_dir)
    l=[li for li in l if li.endswith(endstr)]
    l=[li for li in l if li.startswith(rootstr)]
    l =[image_dir + li for li in l]
    #this list takes all the files in the directory,
    #then only keeps those with the correct beginning and ending

    #l = l[:10]
    #this line can tell the code to only run on the first 10 images
    #useful for debugging
    print("image list: "+str(l))
    lc_out1 = open(lc_out,"w")
    for image_number, li in enumerate(l):
        print('starting photometry on:')
        print li
        print coofnameout
        print phot_out

        #this actually does the photometry
        #note all of the parameters that must be defined
        pyraf.iraf.phot(li,coofnameout, phot_out, plotfile="", datapars="", centerpars="", fitskypars="", photpars="",interactive="no", radplots="no", icommands="", gcommands="", verify="no", weighting="constant", calgorithm="centroid",cbox=10,verbose=True, obstime=obstime, annulus=annulus,dannulus=dannulus,salgorithm='crosscor',skyvalue=skyvalue,fwhmpsf=fwhmpsf,sigma=sigma)

        print("phot done")
        standard_output = sys.stdout
        #saves the current output -ie to the screen
        txdumpfile = open(txdumpout, 'w')
        sys.stdout = txdumpfile
        #directs the output to the txdumpfile
        pyraf.iraf.txdump(phot_out, "otime,mag,merr", "yes", headers="no", parameters="no")
        
        #does the txdump - might you want to output other information?
        sys.stdout = standard_output # restores the stdout
        txdumpfile.close() # close the file
        os.remove(phot_out)
        #deletes the file

        phots  =[]

        for line in open(txdumpout):
            #line=map(string,line.split())
            line=str.split(line)
            phots.append((line[1],line[2]))
            #print phots
        
        #print line[0]

        #lc_out1.write(line[0] + "\t")
        #   print stringtohours(line[0])
        try:
            lc_out1.write( "%10.7f \t" % (stringtohours(line[0])))
        except Exception, err:
            print("could not convert value of"+str(obstime) +"to hours because:"+str(err))
            lc_out1.write( "%10.7f \t" % (image_number))


        for photsi in phots:
            lc_out1.write(photsi[0] + "\t")
            lc_out1.write(photsi[1] + "\t")
        lc_out1.write("\n")

        os.remove(txdumpout)

        

      
            
#r=execute()

